package org.sacgtug.android;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MyActivity2 extends Activity {

	private static final String WEBVIEW_URL = "file:///android_asset/index.html";
	
	private WebView webView;
	
	private Handler mHandler = new Handler();
	
	private MyService myService = null;
	
	private boolean isBound = false;
	private ServiceConnection serviceConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName arg0, IBinder service) {
			Log.i("## A ##", "Remote Service Connected 1");
			myService = MyService.Stub.asInterface(service);
			Log.i("## A ##", "Remote Service Connected 2");
			isBound = true;
		}

		public void onServiceDisconnected(ComponentName arg0) {
			Log.i("## A ##", "Remote Service Disconnected 1");
			myService = null;
			Log.i("## A ##", "Remote Service Disconnected 2");
			isBound = false;
		}
    };
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main);
	    
	    webView = (WebView) findViewById(R.id.webview);
		webView.getSettings().setJavaScriptEnabled(true);
		
		webView.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageFinished(WebView view, String url) {
				
				Log.i("## A ##", "onPageFinished : url = " + url);
			}
		});
		
		webView.loadUrl(WEBVIEW_URL);
		
		webView.addJavascriptInterface(new JavaScriptInterface(), "android");
	}
	
	@Override
    public void onStart() {
    	super.onStart();
    	
    	if(!isBound) {
    		Log.i("## A ##", "Calling bindService() ...");
    		bindService(new Intent(MyService.class.getName()), serviceConnection, Context.BIND_AUTO_CREATE);
    	}
    }
    
    @Override
    public void onPause() {
    	super.onPause();
    	
    	if(isBound) {
    		isBound = false;
    		unbindService(serviceConnection);
    	}
    }
    
    private void callJavaScriptFunction(final String functionName, final String json) {

    	mHandler.post(new Runnable() {

    		public void run() {

    			Log.i("## A ##", "callJavaScriptFunction functionName = " + functionName + " : json = " +json);
    			
    			webView.loadUrl("javascript:" + functionName + "(" + json + ")");
    		}
    	});
    }
    
    public class JavaScriptInterface {

    	public void startMakeServiceCall() {
    		Log.i("## A ##", "Received UI click button event");
    		makeServiceCall();
    	}
    }
    
    private void makeServiceCall() {
    	
   	 String myLocation = null;
        
   	 Log.i("## A ##", "Start service call ...");
   	 
        try {
			myLocation = myService.getLocation();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
		Log.i("## A ##", "Service call result = " + myLocation);
		callJavaScriptFunction("showServiceResult", myLocation);
   }
  
}
